package hipt.com.controller;

import hipt.com.model.Asset;
import hipt.com.model.Category;
import hipt.com.service.AssetService;
import hipt.com.service.CategoryService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private AssetService assetService;	
	@Autowired
	private CategoryService categoryService;	

	@RequestMapping(value = "/assetList")
	public ModelAndView showAssetList(Map model, HttpServletRequest request,
			HttpSession session) {
		/*if ((session.getAttribute("adminEmail")) == null) {
			return new ModelAndView("redirect:adminlogin.html");
		}*/
		List allAssetList = new ArrayList();
		allAssetList = assetService.listAssets();
		
		model.put("assets", allAssetList);
		return new ModelAndView("admin/assetList");
	}
	
	@RequestMapping(value = "/assetDetail", method = RequestMethod.GET)
	public ModelAndView showAsset(Map model, HttpServletRequest request,
			HttpSession session) {
		/*if ((session.getAttribute("adminEmail")) == null) {
			return new ModelAndView("redirect:adminlogin.html");
		}*/
		String assetId = request.getParameter("assetId");

		Asset asset = assetService.getAssetByAssetId(Integer.parseInt(assetId));
		Category cate = categoryService.getCategoryById(asset.getCatId());
		
		model.put("asset", asset);
		model.put("cate", cate);
		return new ModelAndView("admin/assetDetail");
	}
	
	
}
